<template>
  <div class="wrap">
    <pro-table
      ref="proTableInst"
      tableTitle="借用列表"
      :columns="columns"
      :request="fetchBorrowPage"
    >
      <template #operation>
        <n-space>
          <n-button type="primary" @click="handleAddBorrow">新增借用</n-button>
        </n-space>
      </template>
    </pro-table>

    <pro-dialog
      v-model:showDialog="borrowModalShow"
      :modalProps="modalProps"
      :width="1000"
      :showButton="true"
      confirmButtonText="确定"
      :showConfirmButton="handleType !== 'check'"
      :cancelButtonText="handleType === 'check' ? '关闭' : '取消'"
      :contentStyle="{ 'padding-left': '10px', 'padding-right': '10px' }"
      :button-loading="confirmLoading"
      @handleConfirm="borrowConfirm"
      @afterClosed="
        () => {
          resetBorrowForm()
        }
      "
      v-bind="$attrs"
    >
      <pro-form
        ref="borrowFormInst"
        v-model:form="borrowForm"
        :formProps="{
          labelPlacement: 'left',
          labelWidth: '100px'
        }"
        :options="borrowFormOptions"
      ></pro-form>
    </pro-dialog>
  </div>
</template>

<script lang="ts" setup name="Borrow">
  import { ProTableInst } from '@/components/pro-table/type'
  import { ProFormInst } from '@/components/pro-form/type'
  import { useBorrowApi, useBorrowProForm, useBorrowProTable } from './hooks'
  import { BorrowModel } from '@/api/model/borrow-model'
  import { useUser } from '@/store/modules/user'
  import useGetOrgList from '@/hooks/useGetOrgList'

  const proTableInst = ref<ProTableInst | null>(null)
  const borrowFormInst = ref<ProFormInst | null>(null)

  const borrowModalShow = ref(false)
  const handleType = ref<'add' | 'edit' | 'check'>('check')
  const handleTag = computed(() =>
    handleType.value === 'add'
      ? '新增借用'
      : handleType.value === 'check'
      ? '借用详情'
      : '修改借用'
  )
  const modalProps = computed(() => {
    return {
      showIcon: false,
      title: handleTag.value
    }
  })

  const { userInfo } = storeToRefs(useUser())

  const { typeOptions, typeLoading } = useGetTypeList()
  const { fetchGetOrgList, orgFilterOptions, orgOptions, orgLoading } =
    useGetOrgList()

  const { borrowForm, resetBorrowForm, borrowFormOptions } = useBorrowProForm({
    handleType,
    typeOptions,
    typeLoading,
    orgOptions,
    orgFilterOptions,
    orgLoading
  })

  const handleAddBorrow = () => {
    handleType.value = 'add'
    borrowModalShow.value = true
  }

  const handleEditBorrow = (row: BorrowModel, type: 'check' | 'edit') => {
    borrowForm.value = row
    handleType.value = type
    borrowModalShow.value = true
  }

  const borrowConfirm = async () => {
    if (await borrowFormInst.value?.handleSubmit()) {
      fetchAddOrEditBorrow()
    }
  }

  const {
    fetchBorrowPage,
    fetchAddOrEditBorrow,
    fetchDelBorrow,
    confirmLoading
  } = useBorrowApi({
    handleType,
    handleTag,
    borrowModalShow,
    borrowForm,
    proTableInst,
    userInfo,
    orgOptions
  })
  const { columns } = useBorrowProTable({
    typeOptions,
    typeLoading,
    userInfo,
    handleEditBorrow,
    fetchDelBorrow,
    userInfo
  })
</script>

<style lang="less" scoped></style>
